package com.wondersgroup.insurance.dnuilearn.views.pathmeasure;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ViewAnimator;

/**
 * Created by Administrator on 2017/5/22.
 */

public class SearchView1 extends View {

    Paint mPaint = new Paint();
    int cX, cY;
    final int  radius = 30;

    Path mPath =null;

    double sin45= 0;

    RectF reactF = new RectF();

    int lineWidth  = 0;

    final int padding = 100;

    float ratio = 0;
    public SearchView1(Context context) {
        this(context, null);
    }

    public SearchView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(6);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPath = new Path();

        sin45 = Math.sin(45);

        handler.sendEmptyMessageDelayed(100,5000);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startAnimation();
        }
    };

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cX = w/2;
        cY = h/2;

        lineWidth = w - padding*2;
        reactF.set(cX-radius,cY-radius,cX+radius,cY+radius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.GREEN);
//        canvas.draw
        mPath.reset();

//        mPath.addArc(reactF,45,-180);
//        canvas.drawPath(mPath,mPaint);
//        mPath.addCircle(cX,cY,radius, Path.Direction.CW);
//        int delta = (int) (sin45*radius);
//        canvas.save();
//        canvas.translate((float) (ratio*(lineWidth - 4 *radius*sin45)/2.0f),0);
//
//         if (ratio <0.25){
//            mPath.addArc(reactF,45,(4*ratio-1.0f)*360);
//            mPath.moveTo(cX +delta, cY+ delta);
//            mPath.lineTo(cX +delta*2, cY+ delta*2);
//             mPath.lineTo(cX +delta*2 - ratio*lineWidth, cY+ delta*2);
//
//        }else if (ratio < 0.5){
//             int deltaHandle = (int) (delta*(1+(ratio -0.25)/0.25f));
//             mPath.moveTo(cX +deltaHandle, cY+ deltaHandle);
//             mPath.lineTo(cX +delta*2, cY+ delta*2);
//         }else {
//             mPath.moveTo(cX +delta*2, cY+ delta*2);
//         }
//        mPath.lineTo(cX +delta*2 - ratio*lineWidth  , cY+ delta*2);
//        canvas.drawPath(mPath,mPaint);
//        canvas.restore();




        mPath.addCircle(cX,cY,radius, Path.Direction.CW);
        mPaint.setColor(Color.RED);
//        canvas.drawPath(mPath,mPaint);
        PathMeasure pathMeasure = new PathMeasure(mPath,false);
        float length = pathMeasure.getLength();
        Log.e("","length = "+length);
//        stop= length *ratio;
//
//        start = stop * (ratio*ratio*ratio);
        start = length*ratio;
        stop = start +20;
        mPaint.setColor(Color.BLUE);
        dst.reset();
        pathMeasure.getSegment(start,stop,dst,true);
        canvas.drawPath(dst,mPaint);



//        Path path = new Path();
//        // 创建Path并添加了一个矩形
//        path.addRect(-200, -200, 200, 200, Path.Direction.CW);
//
//        Path dst = new Path();
//        // 将 Path 与 PathMeasure 关联
//        PathMeasure measure = new PathMeasure(path, false);
//
//        // 截取一部分存入dst中，并使用 moveTo 保持截取得到的 Path 第一个点的位置不变
////        取值	主要功用
////        true	保证截取得到的 Path 片段不会发生形变
////        false	保证存储截取片段的 Path(dst) 的连续性
//        measure.getSegment(200, 600, dst, true);
//
////        canvas.drawPath(path,mPaint);
//        // 绘制 dst
//        canvas.drawPath(dst, mPaint);


    }
    Path dst = new Path();
    float start, stop;

    public void startAnimation(){
        ValueAnimator valueAnimator = null;
        if (ratio == 0){
            valueAnimator = ValueAnimator.ofFloat(0,1);
        }else {
            valueAnimator = ValueAnimator.ofFloat(1,0);

        }

        valueAnimator.setDuration(8000);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio  = (float) animation.getAnimatedValue();
                invalidate();
            }


        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                handler.sendEmptyMessageDelayed(100,2000);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }
}
